#!/bin/bash

# 配置参数:

# 编译目标输出目录
OutPath="./gate/_bin/"
# 源文件所在目录(如果main.go在当前目录则赋值为空or.)
SrcPath="./gate/main.go"
# 输出目标文件名
Target="gate"
# 程序运行命令行参数
StartComment="$OutPath$Target -logLv=0 -logMode=1"

# 执行任务:

if [ "$1" = "" ]; then
    echo -e "\033[0;31m 未输入环境参数 \033[0m  \033[0;34m {test|online|run|build} \033[0m"
    exit 1
fi

Params=(test,online,run,build)
if echo "${Params[@]}" | grep -w "$1" &>/dev/null; then
	echo "script running for command: <" $1 "> ..."
else
 	echo -e "\033[0;31m 无效的输入参数 \033[0m  \033[0;34m {test|online|run|build} \033[0m"
    exit 1
fi

if [ "$Target" = "" ]; then
    echo -e "\033[0;31m 未填写编译目标 \033[0m"
    exit 1
fi

function do_test_env()
{
    echo -e "target:$Target\033[0;33m build... \033[0m"
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -o $OutPath$Target $SrcPath

    echo -e "build $OutPath$Target done,\033[0;33m ready upload... \033[0m"

	scp $OutPath$Target shane@47.94.42.101:/home/shane/server/$OutPath$Target

	if [ x"$?" == x"0" ]; then
		echo -e "publish $Target to environment[\033[0;36m test \033[0m]\033[0;32m finish \033[0m."
	else
		echo -e "publish $Target to environment[\033[0;36m test \033[0m]\033[0;31m failure \033[0m."
    fi
}

function do_online_env()
{
    echo -e "target:$Target\033[0;33m build... \033[0m"
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -o $OutPath$Target $SrcPath

    echo -e "build $OutPath$Target done,\033[0;33m ready upload... \033[0m"

	scp $OutPath$Target shane@49.233.3.177:/home/shane/server/$OutPath$Target

	if [ x"$?" == x"0" ]; then
		echo -e "publish $Target to environment[\033[0;34m online \033[0m]\033[0;32m finish \033[0m."
	else
		echo -e "publish $Target to environment[\033[0;34m online \033[0m]\033[0;31m failure \033[0m."
    fi
}

function do_local_run()
{
    go build -o $OutPath$Target $SrcPath
    if [ x"$?" == x"0" ]; then
		echo -e "build $OutPath$Target done,\033[0;33m ready start... \033[0m"
        $StartComment
	else
        echo -e "build $OutPath$Target \033[0m\033[0;31m failure \033[0m."
    fi
}

function do_local_build()
{
    go build -o $OutPath$Target $SrcPath
    if [ x"$?" == x"0" ]; then
		echo -e "build $OutPath$Target done,\033[0;33m success \033[0m"
	else
        echo -e "build $OutPath$Target \033[0m\033[0;31m failure \033[0m."
    fi
}
case $1 in
    online)
    do_online_env;;
    test)
    do_test_env;;
    run)
    do_local_run;;
	build)
    do_local_build;;
    *)

esac
